package com.wanmait.zaisousuo.controller;

import com.wanmait.zaisousuo.pojo.CompanyApproval;
import com.wanmait.zaisousuo.pojo.UserType;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.CompanyApprovalService;
import com.wanmait.zaisousuo.service.UserinfoService;
import com.wanmait.zaisousuo.util.CodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/backstage/usertype")
public class UserTypeController
{
    @Resource
    private UserinfoService userinfoService;
    @Resource
    private CompanyApprovalService companyApprovalService;

    //用户管理界面
    @RequestMapping("userList")
    public void userList(Model model)
    {
        List<Userinfo> userinfos = userinfoService.findAll();
        model.addAttribute("userinfos",userinfos);
    }
    //禁用用户
    @RequestMapping("banUser")
    @ResponseBody
    public void banUser(Integer id)
    {
        userinfoService.banUser(id);
    }
    //恢复用户
    @RequestMapping("recoverUser")
    @ResponseBody
    public void recoverUser(Integer id)
    {
        userinfoService.recoverUser(id);
    }
    //显示申请vip的用户(大页面)
    @RequestMapping("checkUser")
    public void checkUser(Model model)
    {

    }

    //显示申请vip的用户(小页面)
    @RequestMapping("checkUserData")
    public void checkUserData(Integer userStates,Model model)
    {
        List<Userinfo> userinfos = userinfoService.findVip(userStates);
        model.addAttribute("userinfos",userinfos);
    }



       //修改是否vip
    @RequestMapping("changeVipStatus")
    @ResponseBody
    public void changeVipStatus(Integer id,Boolean flag)
    {
        userinfoService.changeVipStatus(id,flag);
    }


    //显示申请企业认证的用户(小页面)
    @RequestMapping("companyData")
    @ModelAttribute("userinfos")
    public List<Userinfo> companyData(Integer userStates)
    {
        List<Userinfo>userinfos = userinfoService.findAllCompany(userStates);
        return userinfos;
    }

    //显示申请企业认证的用户(大页面)
    @RequestMapping("company")
    public void company(){}

    //修改是否为公司
    @RequestMapping("changeCompanyStatus")
    @ResponseBody
    public void changeCompanyStatus(Integer id, Boolean flag, HttpSession session){
        userinfoService.changeCompanyStatus(id,flag);
         /*Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
         if(flag==true){
             userinfo.getCompanyApproval().setCompanyChecked("0");
         }
         if(flag==false){
             userinfo.getCompanyApproval().setCompanyChecked("2");
         }*/
    }

    @RequestMapping("picture-show")
    public void pictureShow(){}

    @RequestMapping("companyList")
    public void comapnyList(){}

    //企业认证审核不通过发送邮件
    @RequestMapping("sendEmail")
    @ResponseBody
    public void sendEmail(Integer userid,String email,String phone,String mes,String companyName){
        //mes是发送邮件的内容
        String s ="【再搜索垃圾分类网】对不起，您的企业认证申请未能通过审核。";
        if(mes!=null&&mes!="")s=s+"原因"+mes;
        //字符串s是要发送邮件/短信的内容
        if(email!=null&&email!="")
        {
            //发邮件
            String subject ="再搜索企业认证";
            CodeUtil.sendEmail(email,s,subject);
        }
        else
        {
            //发短信
            CodeUtil.phoneCode(phone,companyName,mes);
        }
    }
}
